  var google = new OAuth2('google', {
    client_id: '814573736694.apps.googleusercontent.com',
    client_secret: 'ky3S6nWdRVhA7HgS_82meJO3',
    api_scope: 'https://picasaweb.google.com/data/',
    response_type: 'code'
  });
  
  var albums = [];
  var photos = [];
  var albums_processed;
  
  function onAlbums(text, xhr) {
    var data = JSON.parse(text);
    for (var i = 0, entry; entry = data.feed.entry[i]; i++) {
      var id = entry['id']['$t'];
      var album = {
        'id' : id.substring(id.lastIndexOf('/')+1), //,id.lastIndexOf('?')
        'title' : entry['title']['$t']
      };

      albums.push(album);
    }
    
    albums_processed = 0;
    for (var i = 0, album; album = albums[i]; i++) {
      getPhotos(album['id']);
    }
  };

  function getAlbums() {
    albums = [];
    photos = [];
    google.authorize(function() {
      var url = "https://picasaweb.google.com/data/feed/api/user/default?alt=json";
      
      var xhr = new XMLHttpRequest();
      xhr.addEventListener('readystatechange', function(event) {
        if (xhr.readyState == 4) {
          if(xhr.status == 200) {
            onAlbums(xhr.responseText, xhr);
          }
        }
      });

      xhr.open('GET', url, true);
      xhr.setRequestHeader('GData-Version', 2);
      xhr.setRequestHeader('Authorization', 'OAuth ' + google.getAccessToken());
      xhr.send();
    });
  };

  function onPhotos(text, xhr) {
    var data = JSON.parse(text);
    if (data.feed.entry) {
      for (var i = 0, entry; entry = data.feed.entry[i]; i++) {
        var photo = {
          'urlthumb' : entry.media$group.media$thumbnail[0].url,
          'urlphoto' : entry.media$group.media$content[0].url,
          'desc' : entry.media$group.media$description.$t,
          'timestamp' : entry.gphoto$timestamp.$t
        };
        photos.push(photo);
      }
    }
  };
  
  function getPhotos(albumid) {
    google.authorize(function() {
      var url = "https://picasaweb.google.com/data/feed/api/user/default/albumid/" + albumid + "?alt=json";
      
      var xhr = new XMLHttpRequest();
      xhr.addEventListener('readystatechange', function(event) {
        if (xhr.readyState == 4) {
          if(xhr.status == 200) {
            onPhotos(xhr.responseText, xhr);
            albums_processed++;
            if (albums_processed == albums.length) {
              // when all albums processed update tabs with calendar
              chrome.windows.getCurrent(function(win) {
                var cwin = win.id;
                chrome.tabs.getAllInWindow(cwin, function(tabs) {
                  for (var i = 0; i < tabs.length; i++) {
                    var t = tabs[i].url;
                    if (t.match('google.com/calendar')) {
                      var tab = tabs[i];
                      chrome.tabs.sendRequest(tab.id, {action: "rewrite"}, function(response) {});
                    }
                  }
                });
              });
            }
          }
        }
      });

      xhr.open('GET', url, true);
      xhr.setRequestHeader('GData-Version', 2);
      xhr.setRequestHeader('Authorization', 'OAuth ' + google.getAccessToken());
      xhr.send();
    });
  };

  function logout() {
    google.clearAccessToken();
  };

